﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMS.DAL.DAHelpers;
using SMS.Entities;
using System.Data;

namespace SMS.DAL
{
    public class ProductDA : BaseDA<Product>
    {
        public override Product SaveObject(string userName, Product tObject)
        {
            #region Procedure

            #endregion
            try
            {
                dbHelper.AddParameter("@Product_ID", tObject.Product_ID);
                dbHelper.AddParameter("@Product_Name", tObject.Product_Name);
                dbHelper.AddParameter("@Product_Desc", tObject.Product_Desc);
                dbHelper.AddParameter("@Product_Category", tObject.Product_Category.Category_ID);
                dbHelper.AddParameter("@Product_Owner", tObject.Product_Owner);
                dbHelper.AddParameter("@IsLive", tObject.IsLive);
                object objID = dbHelper.ExecuteScaler("USP_INS_UPDATE_PRODUCT", System.Data.CommandType.StoredProcedure, System.Data.ConnectionState.Closed);
                if (Convert.ToInt32(objID) > 0)
                    tObject.Product_ID = Convert.ToInt32(objID);
                LogActivity(userName, string.Format("Product Saved with ID {0}", tObject.Product_ID), tObject);
                return tObject;
            }
            catch (Exception ex)
            {
                LogException(userName, ex, tObject);
                throw new Exception("Error Occured while Saving Customer");
            }
        }

        public override bool DeleteObject(string userName, Product tObject)
        {
            throw new NotImplementedException();
        }

        public override List<Product> GetObjectList(string userName, DateTime fromDate, DateTime toDate)
        {
            throw new NotImplementedException();
        }




        public override List<Product> SearchObject(string userName, string SearchText)
        {
            #region Procedure
            /* ALTER PROCEDURE USP_SEARCH_CUSTOMER(@Search_Text varchar(25))
 as
 BEGIN
	SELECT [Customer_Name]
           ,[Customer_Address]
           ,[Customer_Mobile]
           ,[Customer_AlterMobile]
           ,[Customer_CreditLimit]
           ,[IsLive] FROM CUSTOMER
           WHERE [Customer_Name] LIKE '%'+ @Search_Text + '%' 
           OR [Customer_Mobile] LIKE '%'+ @Search_Text + '%' 
 END
             * */
            #endregion

            try
            {
                dbHelper.AddParameter("@SearchText", string.IsNullOrWhiteSpace(SearchText) ? string.Empty : SearchText);
                var reader = dbHelper.ExecuteReader("USP_SEARCH_PRODUCT", System.Data.CommandType.StoredProcedure, System.Data.ConnectionState.Closed);
                var list = new List<Product>();
                while (reader.Read())
                {
                    list.Add(GetProductObject(reader, true));
                }
                LogActivity(userName, string.Format("Searched customer with text {0}", SearchText), null);
                return list;
            }
            catch (Exception ex)
            {
                LogException(userName, ex, null);
                throw new Exception("Error while searching Customer");
            }
        }

        public static Product GetProductObject(IDataReader reader, bool IsFull)
        {
            int i = -1;
            Product obj = new Product();
            if (!reader.IsDBNull(++i))
                obj.Product_ID = reader.GetInt32(i);
            if (!reader.IsDBNull(++i))
                obj.Product_Name = reader.GetString(i);
            if (!reader.IsDBNull(++i))
                obj.Product_Desc = reader.GetString(i);
            if (IsFull)
            {
                i += 1;
                if (!reader.IsDBNull(i) && !reader.IsDBNull(i + 1))
                    obj.Product_Category = new Category { Category_ID = reader.GetInt32(i), Category_Name = reader.GetString(++i) };
                if (!reader.IsDBNull(++i))
                    obj.Product_Owner = reader.GetString(i);
                if (!reader.IsDBNull(++i))
                    obj.IsLive = reader.GetBoolean(i);
            }
            return obj;
        }
    }
}
